#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "SegImageWrapper.h"

#include <QMessageBox>
#include <QCheckBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent), ui(new Ui::MainWindow)
{
    m_ImageIOWizard = new ImageIOWizard;
    m_CoreImageData = new CoreImageData;

    ui->setupUi(this);
    ui->horizontalLayout->addWidget(m_CoreImageData->GetWidget(VisWidgetBase::SLICER_AXIAL));
    ui->horizontalLayout->addWidget(m_CoreImageData->GetWidget(VisWidgetBase::SLICER_SAGITTAL));
    ui->horizontalLayout_2->addWidget(m_CoreImageData->GetWidget(VisWidgetBase::VOLUME));
    ui->horizontalLayout_2->addWidget(m_CoreImageData->GetWidget(VisWidgetBase::SLICER_CORONAL));

    QSpacerItem* verticalSpacer = new QSpacerItem(20, 274, QSizePolicy::Minimum, QSizePolicy::Expanding);

    connect(ui->action_NewImage, SIGNAL(activated()), this, SLOT(OnMenuLoadImage()));

    for (int i = 0; i < m_CoreImageData->GetMaterialsList()->size(); i++)
    {
        QWidget *viewport = AddMaterial((*(m_CoreImageData->GetMaterialsList()))[i]);
        QCheckBox *checkbox = static_cast<QCheckBox*>(viewport->layout()->itemAt(1)->widget());
        ui->scrollAreaWidgetContents->layout()->addWidget(viewport);
        connect(checkbox, SIGNAL(stateChanged(int)), this, SLOT(MaterialChosen(int)));
    }

    ui->scrollAreaWidgetContents->layout()->addItem(verticalSpacer);
    ui->scrollAreaWidgetContents->layout()->setSpacing(3);
}

MainWindow::~MainWindow()
{
    delete ui;
    delete m_ImageIOWizard;
    delete m_CoreImageData;
}

QWidget*
MainWindow::AddMaterial(Material *material)
{    
    QHBoxLayout *hLayout = new QHBoxLayout;
    hLayout->setSpacing(6);

    QWidget* ColorBox = new QWidget(ui->scrollAreaWidgetContents);
    QPalette palette = ColorBox->palette();
    palette.setColor(QPalette::Background, material->GetColor());
    ColorBox->setAutoFillBackground(true);
    ColorBox->setPalette(palette);
    ColorBox->setFixedSize(10,10);
    hLayout->addWidget(ColorBox);

    QCheckBox *MaterialName = new QCheckBox(material->GetName(), ui->scrollAreaWidgetContents);
    MaterialName->setObjectName(material->GetName());
    MaterialName->setFont(QFont("", 8));
    hLayout->addWidget(MaterialName);

    QWidget *viewport = new QWidget;
    viewport->setLayout(hLayout);

    return viewport;
}

void
MainWindow::MaterialChosen(int state)
{
    int index = 0;
    QCheckBox* material = static_cast<QCheckBox*>(this->sender());
    for (int j = 0; j < m_CoreImageData->GetMaterialsList()->size(); j++)
        if ((material->text())==((*(m_CoreImageData->GetMaterialsList()))[j]->GetName()))
            index = j;

    m_CoreImageData->WorkingMaterialChosen(state, (*(m_CoreImageData->GetMaterialsList()))[index]->GetLabel());
}

void
MainWindow::OnMenuLoadImage()
{
   if (UnloadOldImage())
   {
        m_ImageIOWizard->exec();
        if(m_ImageIOWizard->IsImageLoaded())
        {
            m_CoreImageData->LoadMainImage(m_ImageIOWizard->GetImageIO());
            m_ImageIOWizard->ReleaseImage();
        }
    }
}

void
MainWindow::AutomaticSegChecked(bool checked)
{
    if (m_CoreImageData->IsMainImageLoaded())
        m_CoreImageData->SetWorkingSegmentation(SegImageWrapper::THRESHOLD_AUTOMATIC);
    else
        QMessageBox::critical(this, tr("Amaranthus"), tr("Error! No image available. "
                                                         "Please, load the image you want to process."));
}

bool
MainWindow::UnloadOldImage()
{
    if (m_CoreImageData->IsMainImageLoaded())
    {
        int answer = QMessageBox::warning(this,
                                          tr("Amaranthus"),
                                          tr("Loading a new image will discard all the"
                                             "unsaved changes to the current image.\n"
                                             "Do you want to save your changes?"),
                                          QMessageBox::Yes | QMessageBox::Default,
                                          QMessageBox::No,
                                          QMessageBox::Cancel);
        if (answer == QMessageBox::Yes)
        {
            // Show Wizard for saving changes.
            // Here the signal must be emitted to ProjectManager instructing to save image changes.
            return 1;
        }
        else if (answer == QMessageBox::Cancel)
            return 0;
        else if (answer == QMessageBox::No)
            return 1;
    }
    else
        return 1;
}


